<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="队伍名称">
              <a-input v-model="queryParam.name" placeholder="" />
            </a-form-item>
          </a-col>

          <!-- <a-col :md="6" :sm="24">
            <a-form-item label="成立日期">
              <a-date-picker v-model="queryParam.createTime" style="width: 100%" placeholder="请输入成立日期" />
            </a-form-item>
          </a-col> -->

          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item label="申请日期">
              <j-date placeholder="请选择开始日期" class="query-group-cust" v-model="queryParam.createTime_begin"></j-date>
              <span class="query-group-split-cust"></span>
              <j-date placeholder="请选择结束日期" class="query-group-cust" v-model="queryParam.createTime_end"></j-date>
            </a-form-item>
          </a-col>

          <a-col :md="(!advanced && 6) || 24" :sm="24">
            <span
              class="table-page-search-submitButtons"
              :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
            >
              <a-button type="primary" @click="searchQuery">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a-dropdown v-if="selectedRowKeys.length > 0" v-has="'Volunteer:batchdelete'">
                <a-menu slot="overlay">
                  <a-menu-item key="1" @click="batchDel" v-has="'Volunteer:batchdelete'"><a-icon type="delete" />批量删除</a-menu-item>
                </a-menu>
                <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
              </a-dropdown>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table ref="table" size="default" :scroll="{x:true}" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}" :columns="columns" :data="getData" @onSelect="onChange">
      <span slot="action" slot-scope="text, record">
        <a @click="handleReview(record)">查看</a>
        <a @click="handleApply(record)" v-if="record.actStatus==0&&record.directorId==$store.getters.userInfo.id" style="margin-left:10px" >重新提交</a>
        <a @click="handleEdit(record)" v-else v-has="record.isDirector?'Force':'Volunteer:edit'" style="margin-left:10px">编辑</a>
        <a-popconfirm v-if="record.cancelFlag ==1" style="margin-left:10px" title="确定撤销该记录?" @confirm="() => handleRevoke(record.id)">
          <a>撤销</a>
        </a-popconfirm>
        <a-popconfirm
          v-has="record.isDirector?'Force':'Volunteer:delete'"
          style="margin-left:10px"
          title="确定删除?"
          @confirm="() => handleDelete(record.id)"
        >
          <a>删除</a>
        </a-popconfirm>
        <a style="margin-left:10px" @click="handleDeal(record)" v-if="record.dealFlag">审批</a>
      </span>
    </s-table>

    <!-- 社团弹窗 -->
    <VolunteerTeamModal ref="VolunteerTeamModal" @ok="searchQuery"></VolunteerTeamModal>
    
    <!-- 审批 -->
    <Approval v-model="shenpi" :bizId="mdl.id" businessUrl="/VolunteerTeamUser/add" :detailComponent="detailComponent" @ok="searchQuery"></Approval>
    <!-- 查看 -->
    <CheckModal :bizId="detail.id" bizComponent="/volunteer/modules/VolunteerTeam" v-model="detail.visible" ref="CheckModal"></CheckModal>
  
  </a-card>
</template>

<script>
import STable from '@/components/table/'
import ATextarea from 'ant-design-vue/es/input/TextArea'
import AInput from 'ant-design-vue/es/input/Input'
import moment from 'moment'
import XImport from '@/components/extends/XImport.vue'
import JDate from '@/components/jeecg/JDate.vue'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction, postAction, deleteAction } from '@/api/manage'
import { getVolunteerTeams ,setVolunteerTeam,delVolunteerTeam} from '@/api/api'
import VolunteerTeamModal from './modules/VolunteerTeamModal'
import VolunteerTeam from './modules/VolunteerTeam'
import Volunteer from './modules/Volunteer'
import Approval from '@/components/Approval/Approval'
import CheckModal from '@/components/extends/CheckModel'
export default {
  name: 'TableList',
  components: {
    CheckModal,
    JDate,
    AInput,
    ATextarea,
    STable,
    XImport,
    VolunteerTeamModal,
    VolunteerTeam,
    Volunteer,
    Approval
  },
  mixins:[JeecgListMixin],
  data() {
    return {
      detail:{},
      disableMixinCreated:true,
      visibleImport:false,
      detailComponent:VolunteerTeam.name,
      shenpi:false,
      form: null,
      mdl: {},
      url:{
        list:'/volunteerTeam/list',
        add:'/volunteerTeam/add',
        edit:'/volunteerTeam/edit',
        delete:'/volunteerTeam/delete',
        deleteBatch:'/volunteerTeam/deleteBatch',
        exportXlsUrl:''
      },
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        actStatus_MultiString:'-1,0,1'
      },
      // 表头
      columns: [
        {
          title: '队伍名称',
          align: 'center',
          dataIndex: 'name'
        },
        {
          title: '所属部门',
          align: 'center',
          dataIndex: 'departName'
        },
        {
          title: '负责人',
          align: 'center',
          dataIndex: 'creatorName'
        },
        {
          title: '申请时间',
          align: 'center',
          dataIndex: 'createTime',
        },
        {
          title: '当前审核人',
          align:"center",
          width:150,
          dataIndex: 'waitHandler'
        },
        {
          title: '审批状态',
          align: 'center',
          dataIndex: 'actStatus_dictText'
        },
        {
          title: '操作',
          align: 'center',
          // dataIndex: 'action',
          width: '220px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      getData: parameter => {
        let params = Object.assign({},parameter, this.queryParam)
        params.name&&(params.name = `*${params.name}*`)
        return getAction(this.url.list,params).then(res => {
          if(res.success&&res.result){
            const obj = {
              data: res.result.records||[],
              pageNo: res.result.current,
              pageSize: res.result.size,
              totalCount: res.result.total,
              totalPage: res.result.page,
              sortColumns: res.result.sortColumns
            }
            return obj
          }else{
            return {}
          }
        })
      },

      selectedRowKeys: [],
      selectedRows: []
    }
  },
  methods: {
    // 编辑
    handleEdit(record){
      this.$refs.VolunteerTeamModal.title = '编辑'
      this.$refs.VolunteerTeamModal.edit(record);
    },
    // 查看
    handleReview(record){
      // this.$refs.VolunteerTeamModal.review(record);
      this.detail = record
      this.$set(this.detail,'visible',true)
    },
    // 申请
    handleApply(record){
      this.$refs.VolunteerTeamModal.title = '申请志愿者队伍'
      this.$refs.VolunteerTeamModal.apply(record);
    },
    searchReset() {
      this.queryParam = {
        actStatus_MultiString:'-1,0,1'
      }
      this.searchQuery();
    },
    searchQuery() {
      this.$nextTick(() => {
        this.$refs.table.refresh()
      })
    },
    /**
     * 处理
     */
    handleDeal(record) {
      this.mdl = Object.assign({}, record)
      this.shenpi = true
    },
    handleDelete(id){
      delVolunteerTeam({id}).then(res =>{
          if(res.success){
            this.$message.success('删除成功');
            this.searchQuery()
          }else{
            this.$message.error(res.message)
          }
      })
    },
    batchDel: function () {
      if(!this.url.deleteBatch){
        this.$message.error("请设置api.leteBatch属性!")
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！');
        return;
      } else {
        var ids = "";
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ",";
        }
        var that = this;
        this.$confirm({
          title: "确认删除",
          content: "是否删除选中数据?",
          onOk: function () {
            that.loading = true;
            deleteAction(that.url.deleteBatch, {ids: ids}).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.selectedRowKeys = [];
                that.selectionRows = [];
                that.searchQuery()
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.loading = false;
            });
          }
        });
      }
    },
    handleRevoke(id){
      postAction('/workflow/cancelProcess', {
        bizId: id
      }).then(res => {
        if(res.success){
          this.$message.success('撤销成功');
          this.searchQuery()
        }else{
          this.$message.error(res.message)
        }
      })
    },
    onChange(row) {
      this.selectedRowKeys = row.selectedRowKeys
      this.selectedRows = row.selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },

    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
